﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace A2U
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string foldPath = dialog.SelectedPath;
                txtDir.Text = foldPath;
            }
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            //读取所有文件
            DirectoryInfo dir = new DirectoryInfo(txtDir.Text);

            foreach(var item in dir.GetFiles())
            {
                var encoding=GetFileEncodeType(item.FullName);

                if (encoding == Encoding.Default)
                {
                    //读取文件内容
                    string str2 = File.ReadAllText(item.FullName, Encoding.Default);

                    File.WriteAllText(item.FullName, str2, Encoding.UTF8);
                }
            }
        }


        private void Read(string path)
        {
            var list = new List<string>();
            using (StreamReader sr = new StreamReader(path, Encoding.Default))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    list.Add(Convert(line));
                }
            }

            FileStream fs = new FileStream(path, FileMode.CreateNew);
            StreamWriter sw = new StreamWriter(fs);
            foreach(var item in list)
            {
                
            }
            //开始写入
            sw.Write("Hello World!!!!");
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        public System.Text.Encoding GetFileEncodeType(string filename)
        {
            using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                using (System.IO.BinaryReader br = new System.IO.BinaryReader(fs))
                {
                    Byte[] buffer = br.ReadBytes(2);
                    if (buffer[0] >= 0xEF)
                    {
                        if (buffer[0] == 0xEF && buffer[1] == 0xBB)
                        {
                            return System.Text.Encoding.UTF8;
                        }
                        else if (buffer[0] == 0xFE && buffer[1] == 0xFF)
                        {
                            return System.Text.Encoding.BigEndianUnicode;
                        }
                        else if (buffer[0] == 0xFF && buffer[1] == 0xFE)
                        {
                            return System.Text.Encoding.Unicode;
                        }
                        else
                        {
                            return System.Text.Encoding.Default;
                        }
                    }
                    else
                    {
                        return System.Text.Encoding.Default;
                    }
                }
            }
        }

        private string Convert(string content)
        {
            Encoding ascii = Encoding.ASCII;
            Encoding unicode = Encoding.Unicode;

            // Convert the string into a byte[].
            byte[] asciiBytes = ascii.GetBytes(content);

            // Perform the conversion from one encoding to the other.
            byte[]  unicodeBytes  = Encoding.Convert(ascii, unicode, asciiBytes);

            // Convert the new byte[] into a char[] and then into a string.
            // This is a slightly different approach to converting to illustrate
            // the use of GetCharCount/GetChars.
            char[] unicodeChars = new char[unicode.GetCharCount(unicodeBytes, 0, unicodeBytes.Length)];
            unicode.GetChars(unicodeBytes, 0, unicodeBytes.Length, unicodeChars, 0);
            string unicodeString = new string(unicodeChars);

            // Display the strings created before and after the conversion.
            Console.WriteLine("Original string: {0}", content);
            Console.WriteLine("Ascii converted string: {0}", unicodeString);
            return unicodeString;
        }
    }
}
